# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from pymongo import MongoClient


class ZhihuUserPipeline(object):

    def __init__(self):
        self.client = MongoClient()
        self.db = self.client['zhihu']
        self.collection = self.db['user']

    def process_item(self, item, spider):
        self.collection.update({'url_token': item['url_token']}, {'$set': dict(item)}, True)
        return item

    def close_spider(self, spider):
        self.client.close()


